<?php
/**
 * Created by PhpStorm.
 * User: dyane
 * Date: 2018/11/27
 * Time: 14:57
 */

namespace app\admin\controller;

use think\Request;
use think\Validate;
use think\Session;
use think\Db;
class Project extends Base
{
    /**
     * 项目列表页面
     * @return \think\response\View
     */
    public function index()
    {
        //获取管理员信息
        $info = Db::name('admin')->field('id,admin_name')->select();
        $this->assign('info',$info);
        return view();
    }

    public function getList()
    {
        $param = $this->request->param();
        $where = '1 = 1';
        if (!empty($param['name'])) {
            $where .= ' and p.project_name like "%' . $param['name'] . '%"';
        }
        if (!empty($param['admin_id'])) {
            $where .= ' and p.admin_id = ' . $param['admin_id'];
        }
        if (!empty($param['start_time'])) {
            $startTime = strtotime($param['start_time']);
            $where .= ' and p.project_start >= ' . $startTime;
        }
        if (!empty($param['end_time'])) {
            $endTime = strtotime($param['end_time']);
            $where .= ' and p.project_end <= ' . $endTime;
        }
        $count = Db::name('project')->alias('p')->where($where)->join('admin a', 'a.id = p.admin_id')->count();
        $limit = Request::instance()->param('limit');
        $page = Request::instance()->param('page');
        $tol = ($page - 1) * $limit;
        $list = Db::name('project')
            ->field('p.*,a.admin_name')
            ->alias('p')
            ->join('admin a', 'a.id = p.admin_id')
            ->where($where)
            ->limit($tol,$limit)
            ->select();
        //处理数据
        foreach ($list as $key => $value) {
            $list[$key]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
            $list[$key]['project_start'] = date('Y-m-d H:i:s',$value['project_start']);
            $list[$key]['project_end'] = date('Y-m-d H:i:s',$value['project_end']);
        }
//        dump($list);die;
        //返回数据
        return ["code" => "0", "msg" => "", "count" => $count, "data" => $list];
    }
    public function addProjectPage()
    {
        $admin = Db::name('admin')->field('id,admin_name')->select();
        $this->assign('admin',$admin);
        return view();
    }

    public function editProjectPage()
    {
        $param = $this->request->param();
        $projectId = $param['project_id'];
        $info = Db::name('project')->field('p.*,a.admin_name')
            ->alias('p')
            ->join('admin a', 'a.id = p.admin_id')
            ->where(['p.id'=>$projectId])
            ->find();
        $info['project_start'] = date('Y-m-d H:i:s',$info['project_start']);
        $info['project_end'] = date('Y-m-d H:i:s',$info['project_end']);
        $admin = Db::name('admin')->field('id,admin_name')->select();
        $this->assign('admin',$admin);
        $this->assign('info',$info);
        return view();
    }
    public function modifyProject()
    {
        $param = $this->request->param();
        $rule = [
            'admin_id|项目负责人'=>'require',
            'project_name|项目名称'=>'require',
            'project_desc|项目描述'=>'require',
            'project_man|项目预计人数'=>'require|number',
            'cycle|项目周期'=>'require|number',
            'project_start|项目开始时间'=>'require',
            'project_end|项目结束时间'=>'require',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            echo json_encode(['code'=>500,'msg'=>$validate->getError()]);die;
        }
        $data = [
            'admin_id'=>$param['admin_id'],
            'project_name'=>$param['project_name'],
            'cycle'=>$param['cycle'],
            'project_start'=>$param['project_start'],
            'project_end'=>$param['project_end'],
            'project_desc'=>$param['project_desc'],
            'project_man'=>$param['project_man']
        ];
        if(!empty($param['id'])){
            $res = Db::name('project')->where(['id'=>$param['id']])->update($data);
        }else{
            $data['add_time'] = time();
            $res = Db::name('project')->insert($data);
        }
        if($res!==false){
            echo json_encode(['code'=>200,'msg'=>'更新成功','data'=>$res]);die;
        }else{
            echo json_encode(['code'=>500,'msg'=>'error','data'=>$res]);die;
        }
    }
    public function deleteProject()
    {
        $param = $this->request->param();
        $res = Db::name('project')->delete($param['id']);
        if($res){
            echo json_encode(['code'=>200,'msg'=>'删除成功']);die;
        }else{
            echo json_encode(['code'=>500,'msg'=>'删除失败']);die;
        }
    }
}